package controllers;

import play.*;
import play.data.validation.Valid;
import play.libs.Mail;
import play.mvc.*;

import java.util.*;

import models.*;

public class Notifications extends Controller {

	/**
	 * showNotification() gets the user id using the session and then retrieves
	 * the list of notifications according to descending order, which will then
	 * be rendered to be viewed in the showNotification HTML page
	 * 
	 * @author Shahir El Faramawy
	 */

	public static void showNotification() {
		String idString = session.get("userId");
		List<Notification> get = Notification.find(
				"User_id=? order by time desc", idString).fetch();
		render(get);
	}


	/**
	 *gets the user through session,and get his list of notifications,and then loops to delete it all then renders it
	 * in HTML page
	 * 
	 * @Author youstina galal
	 */

	public static void deleteAllNotification() {
		String idstring = session.get("userId");
		Long id = Long.parseLong(idstring);
		User user = User.findById(id);
		List<Notification> notify = Notification.find("byUser_id", user.id)
				.fetch();
		for (int i = 0; i < notify.size(); i++) {
			notify.get(i).delete();
		}

		showNotification();

	}

	/**
	 * takes the notification id in order to get the notification itself and be able to delete it
	 * 
	 * @param notifiesId
	 *            notificatin id
	 * @author youstina galal
	 */
	public static void deleteOneNotification(Long notifiesId) {
		Notification notifys = Notification.findById(notifiesId);
		notifys.delete();
		showNotification();
	}

}